package system.track;

/**
 * This provides special behavior for Block 9.  Block 9 is special because
 * one of its ends is switchless, leading only to Block 8.  The getNextBlock
 * method has been overridden to provide Block 8 directly.  Also, the
 * getters and setters for the switches need to handle this special case.
 *
 * @author David Kolb
 * @version Cycle 1 - complete
 */
public class Block9 extends GenericBlock implements Block
{
    private Block cwBlock;

    /**
     * Constructor for Objects of class Block9.
     */
    public Block9()
    {
        super();
    }

    /**
     * The clockwise Block will always be set to Block 8, there is no
     * other alternative.
     * 
     * @param cwBlock   a Block object 
     */
    public void setCWBlock(Block cwBlock)
    {
        this.cwBlock = cwBlock;
    }

    /**
     * Returns the switch in the specified direction.  Overridden to
     * automatically return a null value for Direction = CLOCKWISE, 
     * because there is no switch in that direction.
     * 
     * @param d the direction to get the switch from.
     * @return The switch to return, null if none.
     */
    @Override
    public Switch getSwitch(Direction d)
    {
        Switch returnVal = null;
        if (d != Direction.CLOCKWISE)
        {
            //use the default method to get the CCW switch
            returnVal = super.getSwitch(d);
        }
        return returnVal;
    }

    /**
     * Registers a switch as being located at the end of the 
     * block in the direction denoted by the argument d. Overridden to
     * automatically only set a switch in the CCW Direction.
     *
     * @param d the direction denoting the end that the switch is on.
     * @param s the switch located at the end of the block.
     */
    @Override
    public void setSwitch(Direction d, Switch s)
    {
        if (d != Direction.CLOCKWISE)
        {
            //use the default method to set the CCW switch
            super.setSwitch(d, s);
        }
    }

    /**
     * This gets the next block in the direction specified.
     *
     * The algorithm for getting the block takes into account the current
     * setting of the switch.  For instance, if the switch at the end is
     * oriented so there are two possible exits and is set to 180 degrees,
     * then the exit at 180 degrees is returned.
     * 
     * This method is overridden in order to only return Block 8 if the
     * Direction is CLOCKWISE.  The usual Block algorithm is run if the 
     * Direction is CCW.
     * 
     * @param d the direction in which to get the next block.
     * @return the next Block
     */
    @Override
    public Block getNextBlock(Direction d)
    {
        Block returnVal = this.cwBlock;
        if (d != Direction.CLOCKWISE)
        {
            //use the default method to get the CCW Block
            returnVal = super.getNextBlock(d);
        }
        return returnVal;
    }
}
